package GameSerializes;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;


public class TDSound implements Serializable {

	private static final long serialVersionUID = 1L;
	private String Name;
	private byte[] stream;
	private transient AudioInputStream Music;

	public TDSound(String path, String ident) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);
		} catch (FileNotFoundException e) {e.printStackTrace();}
		stream = copy(fis).toByteArray();
		Name = ident;
	}

	public String getName() {
		return Name;
	}
	
	public AudioInputStream getAudio() {
		return Music;
	}
	
	public void setup() {
		Music = getMusic(); 
	}

	public Clip play() {
		Clip c = null;
	    try {    
			// load the sound into memory (a Clip)  
			DataLine.Info info = new DataLine.Info(Clip.class, Music.getFormat());  
			c = (Clip) AudioSystem.getLine(info);  
			c.open(Music);   
			// play the sound clip  
			c.start();  
			c.addLineListener(new LineListener() {
				public void update(LineEvent event) {
					if (event.getType().equals(LineEvent.Type.STOP)) {
			            event.getLine().close();
			        }
				}
			});
	    }
	    catch (Exception e) {  
	    	e.printStackTrace();  
	    }  
	    setup();
	    return c;
		//AudioPlayer player = AudioPlayer.player;
		//player.start(Music);
	}

	public Clip play(float volume) {
		FloatControl gainControl;
		Clip c = null;
	    try {    
			// load the sound into memory (a Clip)  
			DataLine.Info info = new DataLine.Info(Clip.class, Music.getFormat());  
			c = (Clip) AudioSystem.getLine(info);  
			c.open(Music);   
			// change volume
			gainControl = (FloatControl)c.getControl(FloatControl.Type.MASTER_GAIN);
	        gainControl.setValue(volume);
			// play the sound clip  
			c.start();  
			c.addLineListener(new LineListener() {
				public void update(LineEvent event) {
					if (event.getType().equals(LineEvent.Type.STOP)) {
			            event.getLine().close();
			        }
				}
			});
	    }
	    catch (Exception e) {  
	    	e.printStackTrace();  
	    }  
	    setup();
	    return c;
		//AudioPlayer player = AudioPlayer.player;
		//player.start(Music);
	}
	
    public AudioInputStream getMusic(){
        try {
        	final InputStream is = new ByteArrayInputStream(stream);
            final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
            return audioInputStream;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	private ByteArrayOutputStream copy(FileInputStream fis) {
	    byte[] buf = new byte[8192];
	    ByteArrayOutputStream os = new ByteArrayOutputStream();
	    while (true) {
	    	int length = 0;
			try {
				length = fis.read(buf);
			} catch (IOException e) {e.printStackTrace();}
			if (length < 0)
				break;
			os.write(buf, 0, length);
		}
	    return os;
	}
	
}
